//index.js
const app = getApp()

Page({
  data: {
    // 用于分页的属性
    totalPage: 1,
    page: 1,
    videoList:[],

    screenWidth: 350,
    serverUrl: "",
    searchContent: "",
    empty:""
  },

  onLoad: function (params) {
    var me = this;
    var screenWidth = wx.getSystemInfoSync().screenWidth;
    me.setData({
      screenWidth: screenWidth,
    });

    var searchContent = params.search;
    var isSaveRecord = params.isSaveRecord;
    // debugger;
    if (isSaveRecord == null || isSaveRecord == '' || isSaveRecord == undefined){
      isSaveRecord = 0;
    }
    
    me.setData({
      searchContent: searchContent
    });
    // 获取当前的分页数
    var page = me.data.page;
    me.getAllVideoList(page,isSaveRecord);
  },
  getAllVideoList: function (page, isSaveRecord){
    var me = this;
    var serverUrl = app.serverUrl;
    wx.showLoading({
      title: '请等待，加载中....',
    });

    var searchContent = me.data.searchContent;
    wx.request({
      url: serverUrl + '/video/showAll?page=' + page + "&isSaveRecord=" + isSaveRecord,
      method:'POST',
      data:{
        videoDesc: searchContent,
      },
      success: function (res) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        //加载完毕后，停止下拉刷新
        wx.stopPullDownRefresh();
        // 判断当前页 page 是否是第一页，如果是第一页，那么设置 videoList 为空
        if (page == 1) {
          me.setData({
            videoList: []
          });
        }
     
        var empty=''
        var videoList = res.data.data.rows;
        if(videoList.length==0){
              empty= '暂无数据哦!!';
        }
        var newVideoList = me.data.videoList;
        // debugger
        me.setData({
          empty:empty,
          videoList: newVideoList.concat(videoList),
          page: page,
          totalPage: res.data.data.total,
          serverUrl: serverUrl
        });
      }
    });
  },
  onPullDownRefresh: function(){
    console.log('监听用户下拉刷新事件。')
     //监听用户下拉刷新事件。
      wx.showNavigationBarLoading();
      this.getAllVideoList(1,0);
  },

  onReachBottom: function(){
    console.log('监听用户上拉触底事件')
    //监听用户上拉触底事件。
    var me = this;
    var currentPage = me.data.page;
    var totalPage = me.data.totalPage;

    // 判断当前页数和总页数是否相等，如果相等则无需查询
    if (currentPage == totalPage){
      wx.showToast({
        title: '已经没有视频啦~~',
        icon: "none"
      });
      return;
    }

    var page = currentPage + 1;

    me.getAllVideoList(page,0);
  },

  showVideoInfo: function(e){
    // debugger
    var me = this;
    var videoList = me.data.videoList;
    var arrindex = e.target.dataset.arrindex;
    var videoInfo = JSON.stringify(videoList[arrindex]);

    wx.redirectTo({
      url:'../videoinfo/videoinfo?videoInfo=' + videoInfo
    })
  }
})

